package net.akkz.dao.impl;

import java.util.ArrayList;

import net.akkz.dao.FultureJoinDao;
import net.akkz.dbFactory.Session;
import net.akkz.exception.BuildClassError;
import net.akkz.exception.SQLExcuteException;
import net.akkz.model.FultureJoined;
import net.akkz.model.MyString;

public class FultureJoinDaoImpl implements FultureJoinDao
{
	@Override
	public void addFultureJoined(Session session, FultureJoined fultureJoined) throws SQLExcuteException
	{
		String sql = "insert into fulturejoined(fultureid, studentid) values(?, ?)";
		
		Object[] values = new Object[2];
		values[0] = fultureJoined.getFultureid();
		values[1] = fultureJoined.getStudentid();
		
		session.excute(sql, values);
	}

	@Override
	public ArrayList<FultureJoined> findFultureJoinedByFultureId(Session session, int fultureId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from fulturejoined where fultureid = ?";
		
		Object[] values = new Object[]{ fultureId };
		
		return session.find(sql, values, FultureJoined.class);
	}

	@Override
	public ArrayList<FultureJoined> findFultureJoinedByStudentId(Session session, int studentId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from fulturejoined where studentid = ?";
		
		Object[] values = new Object[]{ studentId };
		
		return session.find(sql, values, FultureJoined.class);
	}

	@Override
	public void delFultureJoined(Session session, FultureJoined fultureJoined) throws SQLExcuteException
	{
		String sql = "delete from fulturejoined where fultureid = ? and studentid = ?";
		
		Object[] values = new Object[2];
		values[0] = fultureJoined.getFultureid();
		values[1] = fultureJoined.getStudentid();
		
		session.excute(sql, values);
	}

	@Override
	public boolean checkFultureJoined(Session session, FultureJoined fultureJoined) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from fulturejoined where fultureid=? and studentid = ?";
		
		Object[] values = new Object[]{ fultureJoined.getFultureid(),fultureJoined.getStudentid() };
		
		FultureJoined fultureJoinedModel = session.get(sql, values, FultureJoined.class);
		
		return fultureJoinedModel != null;
	}

	@Override
	public ArrayList<MyString> findFultureJoinedNameByFultureId(Session session, int fultureId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select student.name from fulturejoined,student where fulturejoined.studentid=student.id and fulturejoined.fultureid = ?";
		
		Object[] values = new Object[]{ fultureId };
		
		return session.find(sql, values, MyString.class);
	}
}
